package org.langqiao.network.java复习题老师给的;
/*import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
*/
import java.io.*;
import java.net.*;
public class Client extends Thread  {
   public Client(){}//构造方法
   public void run() 
   {
       Socket socket = null;
	   InputStream in = null;
	   OutputStream out = null;
	   BufferedReader buffer = null;
	   System.out.print("请输入服务器地址：");
	   try
	   {
	      String serverip = new BufferedReader(new InputStreamReader(System.in)).readLine(); //得到输入的服务器地址
	      socket = new Socket();  //建立Socket连接
	      socket.connect(new InetSocketAddress(serverip,9000)); //连接到服务器
	      if(socket.isConnected())
	      {
	          System.out.println("连接中...");
	      }
	      in = socket.getInputStream(); //得到输入流
	      out = socket.getOutputStream(); //得到输出流
	   }
	   catch (Exception e)
	   {
	      //e.printStackTrace();
	   }
	   while(true)
	   {
		   // 发送
		     try 
		     {
			    System.out.print("客户机消息：");
			    buffer = new BufferedReader(new InputStreamReader(System.in));
			    String outstr = buffer.readLine(); //得到输入的消息
			    byte[] send = outstr.getBytes();
			    out.write(send);     //向服务器发送消息
		     }
		     catch (Exception e) 
		     {
		         //e.printStackTrace();
		     }
		   //接收
		    try 
		    {
			    byte[] receive = new byte[200];
			    in.read(receive);     //读出服务器发来的消息
			    String instr = new String(receive);
			    System.out.print("服务器消息：" + instr.trim() + "\n"); //打印服务器消息
		    } 
		    catch (Exception e) 
		    {
			    // 错误处理
			    //e.printStackTrace();
		    }
	   }
 
  }
  public static void main(String[] args) 
  {
	  new Client().start(); //启动客户端线程
  }
}


